package com.talkingdata.hadoop.guard.mapper;

import com.talkingdata.hadoop.guard.bean.RmState;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface RmMapper {
    @Insert("insert into rm_state (cluster_id,rm_id," +
            "hostname,http_address,service_state,timestamp) " +
            "values (#{clusterId},#{rmId},#{hostname}," +
            "#{httpAddress},#{serviceState},#{timestamp})")
    int insertRm (RmState rmState);

    @Select("select * from rm_state " +
            "where cluster_id = #{cluster_id} and " +
            "rm_id = #{rm_id} order by id DESC limit 1 ")
    @Results({
            @Result(property = "clusterId",column = "cluster_id"),
            @Result(property = "rmId",column = "rm_id"),
            @Result(property = "httpAddress",column = "http_address"),
            @Result(property = "serviceState",column = "service_state")
    })
    RmState selectRmStatebyRmid(@Param("cluster_id") String cluster_id, @Param("rm_id") String rm_id);

    @Select("select * from rm_state " +
            "where cluster_id = #{cluster_id} and " +
            "service_state = 'ACTIVE' order by id DESC limit 1 ")
    @Results({
            @Result(property = "clusterId",column = "cluster_id"),
            @Result(property = "rmId",column = "rm_id"),
            @Result(property = "httpAddress",column = "http_address"),
            @Result(property = "serviceState",column = "service_state")
    })
    RmState selectActiveRMStatebyClusterId(@Param("cluster_id") String cluster_id);


    @Select("select * from rm_state " +
            "where cluster_id = #{cluster_id} and " +
            "timestamp = (select timestamp from rm_state where cluster_id = #{cluster_id} order by id DESC limit 1)")
    @Results({
            @Result(property = "clusterId",column = "cluster_id"),
            @Result(property = "rmId",column = "rm_id"),
            @Result(property = "httpAddress",column = "http_address"),
            @Result(property = "serviceState",column = "service_state")
    })
    ArrayList<RmState> selectRMStatebyClusterId(@Param("cluster_id") String cluster_id);

}
